package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseExpressOrgItemMapper;
import com.tsd.base.entity.BaseExpressOrgExt;
import com.tsd.base.entity.BaseExpressOrgItem;
import com.tsd.base.entity.BaseExpressOrgItemExt;
import com.tsd.base.service.BaseExpressOrgItemService;
import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUserExt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseExpressOrgItem 服务实现层
 *
 * @author Hillpool
 * @date 2021/04/15 11:17:56
 */
@Service
public class BaseExpressOrgItemServiceImpl extends BaseServiceImpl implements BaseExpressOrgItemService {

    @Resource
    private BaseExpressOrgItemMapper baseExpressOrgItemMapper;

    @Override
    public BaseExpressOrgItem getBaseExpressOrgItemById(Long id) throws Exception {
        return baseExpressOrgItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseExpressOrgItemExt queryBySid(String sid) throws Exception {
        return baseExpressOrgItemMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseExpressOrgItem(Long id, SysUserExt opUser) throws Exception {
        BaseExpressOrgItem old = baseExpressOrgItemMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseExpressOrgItemMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void saveBaseExpressOrgItem(BaseExpressOrgItemExt record, SysUserExt opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseExpressOrgItemMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseExpressOrgItemMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveBaseExpressOrgItem(BaseExpressOrgExt record, SysUserExt opUser) throws Exception {
        List<BaseExpressOrgItemExt> itemExtList = record.getItem_list();
        if (HlpUtils.isEmptyList(itemExtList)) {
            return;
        }
        List<Long> existIdList = ListUtil.filterMap(itemExtList, t -> !HlpUtils.isEmpty(t.getId()), BaseExpressOrgItemExt::getId);
        BaseExpressOrgItemExt params = new BaseExpressOrgItemExt();
        params.setDr(0);
        params.setExpress_sid(record.getSid());
        List<BaseExpressOrgItemExt> oldList = baseExpressOrgItemMapper.selectByEntity(params);
        List<Long> delIds = new ArrayList<>();
        if (!HlpUtils.isEmptyList(oldList)) {
            delIds = ListUtil.filterMap(oldList, t -> !existIdList.contains(t.getId()), BaseExpressOrgItemExt::getId);
        }
        if (!HlpUtils.isEmptyList(delIds)) {
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIds);
            baseExpressOrgItemMapper.deleteLogicByIdList(logicVo);
        }
        List<BaseExpressOrgItemExt> insertList = new ArrayList<>();
        List<BaseExpressOrgItemExt> updateList = new ArrayList<>();
        for (BaseExpressOrgItemExt itemExt : itemExtList) {
            super.checkEmpty(itemExt.getMin_value(), "起始重量不能为空");
            super.checkEmpty(itemExt.getWeight_grad(), "重量梯度不能为空");
            super.checkEmpty(itemExt.getValuation_mode(), "计价模式不能为空");
            if (BaseExpressOrgItemExt.MODE_FREIGHT.equals(itemExt.getValuation_mode())) {
                super.checkEmpty(itemExt.getFreight(), "计价模式为固定运费，固定运费不能为空");
            } else if (BaseExpressOrgItemExt.MODE_PRICE.equals(itemExt.getValuation_mode())) {
                super.checkEmpty(itemExt.getPrice(), "计价模式为固定单价，运费单价不能为空");
            }
            if (!HlpUtils.isEmpty(itemExt.getMax_value()) && itemExt.getMax_value() < itemExt.getMin_value()) {
                throw new HlpException("截止重量不能小于起始重量");
            }
            itemExt.setExpress_sid(record.getSid());
            if (HlpUtils.isEmpty(itemExt.getId())) {
                super.autoInjectBaseData(itemExt, opUser, this.TYPE_CREATE);
                insertList.add(itemExt);
            } else {
                super.autoInjectBaseData(itemExt, opUser, this.TYPE_UPDATE);
                updateList.add(itemExt);
            }
            if (insertList.size() == 100) {
                baseExpressOrgItemMapper.insertList(insertList);
                insertList.clear();
            }
            if (updateList.size() == 100) {
                baseExpressOrgItemMapper.updateList(updateList);
                updateList.clear();
            }
        }
        if (!HlpUtils.isEmptyList(insertList)) {
            baseExpressOrgItemMapper.insertList(insertList);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            baseExpressOrgItemMapper.updateList(updateList);
        }
    }

    @Override
    public List<BaseExpressOrgItemExt> findBaseExpressOrgItems(Map<String, Object> params, PageBean page) throws Exception {
        return baseExpressOrgItemMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseExpressOrgItemExt> findBaseExpressOrgItems(Map<String, Object> params) throws Exception {
        return baseExpressOrgItemMapper.selectByParams(params);
    }

    @Override
    public List<BaseExpressOrgItemExt> queryByEntity(BaseExpressOrgItemExt params) throws Exception {
        return baseExpressOrgItemMapper.selectByEntity(params);
    }
}
